package com.cheyun.netsalev3.tencenttim.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.tencenttim.BaseActivity;
import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.tencenttim.utils.Constants;
import com.cheyun.netsalev3.tencenttim.utils.DemoLog;
import com.cheyun.netsalev3.utils.MyAudioPlayer;
import com.cheyun.netsalev3.widget.floatingview.FloatingView;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;

import androidx.annotation.Nullable;
//import floatwindow.xishuang.float_lib.FloatActionController;

public class ChatActivity extends BaseActivity {

    private static final String TAG = ChatActivity.class.getSimpleName();

    private ChatFragment mChatFragment;
    public static ChatInfo mChatInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_activity);

        chat(getIntent());

        MyApplication.getInstance().addChatActivity(this);

//        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        //windowSoftInputMode 属性设置为 stateHidden 软键盘隐藏
        FloatingView.get().getView().setVisibility(View.GONE);
//        FloatActionController.getInstance().hide();
        MyApplication.getInstance().setEnableShowMsg(false);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        DemoLog.i(TAG, "onNewIntent");
        super.onNewIntent(intent);
        chat(intent);
    }

    @Override
    protected void onResume() {
        DemoLog.i(TAG, "onResume");
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().setEnableShowMsg(true);
        FloatingView.get().show();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        DemoLog.i(TAG, "onBackPressed");
        mChatFragment.finishVideoCall();
    }

    private void chat(Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            finish();
        } else {
            mChatInfo = (ChatInfo) bundle.getSerializable(Constants.CHAT_INFO);
            if (mChatInfo == null) {
                finish();
                return;
            }
            mChatFragment = new ChatFragment();
            mChatFragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.empty_view, mChatFragment).commitAllowingStateLoss();
        }
    }

    public void closeCall(){
//        CustomAVCallUIController.getInstance().close();
        MyAudioPlayer.getInstance().stopPlay();
        finish();
//        CustomAVCallUIController.getInstance().hangup();
    }

    public void startCall(int roomId,int chatType){
        mChatFragment.startCall(roomId,chatType);
    }
}
